package com.shujia.student.code;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;

public class Demo1 {

    public static void main(String[] args) throws Exception {

        /**
         *
         * 1、统计年级排名前十学生各科的分数 [学号,学生姓名，学生班级，科目名，分数]
         *
         */

        //1、计算学生总分
        BufferedReader bufferedReader = new BufferedReader(new FileReader("java/data/score.txt"));


        HashMap<String, Integer> hashMap = new HashMap<String, Integer>();

        String line;
        while ((line = bufferedReader.readLine()) != null) {
            String[] split = line.split(",");
            String id = split[0];
            int s = Integer.parseInt(split[2]);

            Integer integer = hashMap.get(id);
            if (integer == null) {
                hashMap.put(id, s);
            } else {
                hashMap.put(id, integer + s);
            }
        }


        //2、取总分排名前十的学生
        ArrayList<String> top10 = new ArrayList<String>();

        for (Map.Entry<String, Integer> entry : hashMap.entrySet()) {
            String id = entry.getKey();
            Integer sumScore = entry.getValue();

            top10.add(id + "," + sumScore);
        }


        //排序
        top10.sort(new Comparator<String>() {
            public int compare(String o1, String o2) {
                int s1 = Integer.parseInt(o1.split(",")[1]);
                int s2 = Integer.parseInt(o2.split(",")[1]);

                return s2 - s1;
            }
        });

        //取前10
        List<String> top = top10.subList(0, 10);
        ArrayList<String> newTop10 = new ArrayList<>();
        for (String s : top) {
            String id = s.split(",")[0];
            newTop10.add(id);
        }


        /**
         * 整理数据
         *
         */


        //将学生表读取到内存中转换成一个mao集合，以学生编号作为key,学生信息作为value
        BufferedReader bufferedReader2 = new BufferedReader(new FileReader("java/data/students.txt"));
        HashMap<String, String> studentMap = new HashMap<>();
        String line2;
        while ((line2 = bufferedReader2.readLine()) != null) {
            String id = line2.split(",")[0];
            studentMap.put(id, line2);
        }


        BufferedReader bufferedReader3 = new BufferedReader(new FileReader("java/data/cource.txt"));
        HashMap<String, String> courceMap = new HashMap<>();
        String line3;
        while ((line3 = bufferedReader3.readLine()) != null) {
            String cid = line3.split(",")[0];
            String cName = line3.split(",")[1];
            courceMap.put(cid, cName);
        }


        //2、关联学生表和分数表
        BufferedReader bufferedReader1 = new BufferedReader(new FileReader("java/data/score.txt"));

        String line1;

        while ((line1 = bufferedReader1.readLine()) != null) {

            String id = line1.split(",")[0];
            String cource = line1.split(",")[1];
            String score = line1.split(",")[2];

            //判断学生是不是前十的学生
            if (newTop10.contains(id)) {

                //通过学生获取学生信息
                String stuInfo = studentMap.get(id);

                //通过科目编号获取科目名
                String cName = courceMap.get(cource);

                System.out.println(stuInfo + "," + cName + "," + score);

            }
        }
    }
}
